package org.example.Server.service.unit.impl;

import lombok.extern.slf4j.Slf4j;
import org.example.Server.mapper.unit.UnitTeachMapper;
import org.example.Server.model.unit.Unit;
import org.example.Server.model.unit.UnitReDTO;
import org.example.Server.service.unit.UnitTeachService;
import org.example.Server.utils.ResultAPI;
import org.example.Server.utils.SreamUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static org.example.Server.utils.ExceptionEnum.SQL_ERROR;

@Slf4j
@Service
public class UnitTeachServiceImpl implements UnitTeachService {
    @Autowired
    SreamUtils sreamUtils;
    @Autowired
    UnitTeachMapper unitTeachMapper;

    @Override
    public ResultAPI addUnitRe(UnitReDTO unitReDTO) {
        ResultAPI resultAPI = null;
        try {
            unitTeachMapper.addUnitRe(unitReDTO);
            resultAPI = ResultAPI.success();
        } catch (Exception e) {
            e.printStackTrace();
//            log.error(e.getMessage());
            resultAPI = ResultAPI.error(SQL_ERROR.getResultCode(), SQL_ERROR.getResultMsg());
        } finally {
            return resultAPI;
        }
    }

    @Override
    public ResultAPI updateUnit(Unit unit) {
        ResultAPI resultAPI = null;
        try {
            unitTeachMapper.updateUnit(unit);
            resultAPI = ResultAPI.success();
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            resultAPI = ResultAPI.error(SQL_ERROR.getResultCode(), SQL_ERROR.getResultMsg());
        } finally {
            return resultAPI;
        }
    }

    @Override
    public ResultAPI deleteUnits(List<Integer> ids) {
        ResultAPI resultAPI = null;
        try {
            unitTeachMapper.deleteUnits(ids);
            resultAPI = ResultAPI.success();
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            resultAPI = ResultAPI.error(SQL_ERROR.getResultCode(), SQL_ERROR.getResultMsg());
        } finally {
            return resultAPI;
        }
    }

    @Override
    public ResultAPI delUnitRes(List<Integer> unitReIds) {
        ResultAPI resultAPI = null;
        try {
            unitTeachMapper.delUnitRes(unitReIds);
            resultAPI = ResultAPI.success();
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            resultAPI = ResultAPI.error(SQL_ERROR.getResultCode(), SQL_ERROR.getResultMsg());
        } finally {
            return resultAPI;
        }
    }

    @Override
    public ResultAPI addUnit(Unit unit) {
        ResultAPI resultAPI = null;
        try {
            unitTeachMapper.addUnit(unit);

            String intro = sreamUtils.getIntro(unit.getSemester_id(), 100);

            System.out.println(intro);
            unitTeachMapper.updateSemIntro(unit.getSemester_id(), intro);
            resultAPI = ResultAPI.success();
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            resultAPI = ResultAPI.error(SQL_ERROR.getResultCode(), SQL_ERROR.getResultMsg());
        } finally {
            return resultAPI;
        }
    }
}
